package com.yan.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.yan.commonutils.R;
import com.yan.servicebase.exceptionhandler.GuliException;
import com.yan.vod.Utils.ConstantVodUtils;
import com.yan.vod.Utils.InitVideoClient;
import com.yan.vod.service.VodService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin

public class VodController {

    @Autowired
    private VodService vodService;
//    上传视频到阿里云的方法
    @ApiOperation(value = "上传视频到阿里云的方法")
    @PostMapping("uploadAliyunVideo")
    public R uploadAliyunVideo(MultipartFile file){
       String videoId= vodService.uploadVideoAliyun(file);

        return R.ok().data("videoId",videoId);
    }
    @ApiOperation(value = "根据视频id删除视频")
    @DeleteMapping("removeAlyVideo/{id}")
    public R removeVideo(@PathVariable String id){
        try{
            DefaultAcsClient client =
                    InitVideoClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(id);
            client.getAcsResponse(request);
            return R.ok();

        }catch(Exception e){
            e.printStackTrace();
            throw new GuliException(20001,"删除视频失败！");

        }

    }
    @ApiOperation(value = "删除多个阿里云视频的方法")
    @DeleteMapping("delete-batch")
    public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList){
        vodService.removeMoreAlyVideo(videoIdList);

        return R.ok();
    }





}
